package com.Akronmcp.hopMessage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import com.Akronmcp.hopTextAPI.p2.HManager;

/**指令处理类*/
public class HmComer implements CommandExecutor,TabCompleter
{

	static HManager hta = HmM.hta;
	static FileConfiguration lang = HmM.lang;
	
	
	String[] subCommands = {"reload", "version","help","on","off","send"};

	//各种子指令
	
	@Override
    public boolean onCommand(CommandSender sd, Command cmd, String label, String[] args)
    {
        lang = HmM.lang;
       
       if(cmd.getName().equalsIgnoreCase("hm"))
       {//主指令
    	   
    	    Player p = null;
	        if(sd instanceof Player)
	        {
	    	   p = (Player)sd;
	        }
	        
    	   if(args.length==0)//----
    	   {
    		   if(p==null)
    		   {
    			   sd.sendMessage(help1());
    		   }else
    		   {
    			   hta.getHopChat("help1").send(p);
    		   }
    		   
    		   return true;
    	   }
    	   
    	   String cmda1= args[0].toLowerCase();	
    	   
    	   //规范化指令参数位置(前过滤)
    	   if(args.length==1)//-----------------   
    	   {//判断操作类型(参数1)
	    	   switch(cmda1)
	    	   {
	    	     case "reload":
	    	      try 
	    	      {
	    	         	new HmLoader();
	    	         	//载入配置
	    	         	 sd.sendMessage(help("loadok"));

	    	      }catch (Exception e) 
	    	      {
	    	    	     e.printStackTrace();
	    	        	 sd.sendMessage(help("loadnotok"));
	    	 	  }
	    		 break;
	    	     case "version":
	    		 sd.sendMessage("§ahopMessage 1.0.0 by Akr_on");
	    		 break;
	    		 
	    		 default:
	    		   if(p==null)
	      		   {
	      			   sd.sendMessage(help1());
	      		   }else
	      		   {
	      			   hta.getHopChat("help1").send(p);
	      		   }
	    	     return true;
	    	   }
	    	   
	    	   return true;
    	   }
    	   
    	  
    	   if(args.length>1)//----------------------
    	   {//若存在id(参数2)
    		 String id = args[1];
    		 
    		 if(args.length > 2)
    		 {//若存在玩家id(参数3)
    			
				 Player p2 =Bukkit.getPlayer(args[2]);
				 if(p2!=null&&p2.isOnline())
				 {
					 p = p2;
				 }else
				 {
					 sd.sendMessage(help("playeroffline"));
    			     return true;
				 }
    			 
    			 
    		 }else if(p == null)
    		 {//若无玩家id且为控制台
    			 sd.sendMessage(help("usebyplayer"));
    			 return true;
    		 }
    		 
	    	   switch(cmda1)
	    	   {//按照操作类型(参数1)处理
	    	     case "send":
	    	     if(p!=null)
	    	     HopMess.hmMap.get(id).send(p);
	    	     break;
	    	     case "on":
	    	     HopMess.hmMap.get(id).setEnable(true);
	    	     break;
	    	     case "off":
	    	     HopMess.hmMap.get(id).setEnable(false);
	        	 break;
	    	    
	    	     default:
	    	     sd.sendMessage(help1());
				 return true;
	    	   }
	    	   
	    	    return true;
            }
     
    	}
       
       return false;
    }

	public String help(String path)
	{
		return lang.getString(path,"未找到节点"+path);
	}
	
	private String help1()
	{
		return lang.getString("help1","未找到help1语言文本");
	}
	
	/**指令补全*/
	@Override
    public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) 
    {//tab补全处理
    	 final List<String> list = new ArrayList<String>();
    	 //初定返回值
    	 
    	if(cmd.getName().equals("hm"))
    	{
    		//如果不是能够补全的长度，则返回空列表(玩家列表)
        if (args.length > 2) return null;

        
		//如果此时仅输入了命令"hta"，则直接返回所有的子命令
        if (args.length == 0) return Arrays.asList(subCommands);

       //筛选所有可能的补全列表方法2
       // return Arrays.stream(subCommands).
       // filter(s -> s.startsWith(args[0])).collect(Collectors.toList());
        
        if(args.length == 1) 
        {
        	 for (String part : subCommands) 
             {
                 if (part.toLowerCase().startsWith(args[0].toLowerCase()))
                 {
                     list.add(part);
                 }
             }
        }//筛选参数1的匹配
        
        
        if(args.length == 2)
        {
        	if(args[0].equals("send")||args[0].equals("on")||args[0].equals("off")) 
        	{
        		if(!HopMess.hmMap.isEmpty())
	        	for (String tid: HopMess.hmMap.keySet()) 
	            {
	                if (tid.toLowerCase().startsWith(args[1].toLowerCase()))
	                {
	                    list.add(tid);
	                }
	            }//匹配集合的hopmess
        		
        	}
        	
         }//筛选参数2的匹配

       
    	}
    	
        return list;
        //返回筛选指令
    	
    }
     

	
}
